﻿using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;

namespace SignalRServer
{
    /// <summary>
    /// 日 期：2015.11.25 15:48
    /// 描 述：即使通信服务
    /// 可供客户端调用的方法开头用小写
    /// </summary>
    public class Chats : Hub
    {

        public async Task SendMessage(string user, string message)
        {
            await Clients.All.SendAsync("ReceiveMessage", user, message);
        }

        public override async Task OnConnectedAsync()
        {
            await Clients.All.SendAsync("Send", $"{Context.ConnectionId} joined");
        }

        public override async Task OnDisconnectedAsync(Exception ex)
        {
            await Clients.All.SendAsync("Send", $"{Context.ConnectionId} left");
        }

        public Task Send(string message)
        {
            return Clients.All.SendAsync("Send", $"{Context.ConnectionId}: {message}");
        }

        public Task SendToGroup(string groupName, string message)
        {
            return Clients.Group(groupName).SendAsync("Send", $"{Context.ConnectionId}@{groupName}: {message}");
        }

        //public async Task JoinGroup(string groupName)
        //{
        //    await Groups.SendAsync(Context.ConnectionId, groupName);

        //    await Clients.Group(groupName).SendAsync("Send", $"{Context.ConnectionId} joined {groupName}");
        //}

        //public async Task LeaveGroup(string groupName)
        //{
        //    await Groups.SendAsync(Context.ConnectionId, groupName);

        //    await Clients.Group(groupName).SendAsync("Send", $"{Context.ConnectionId} left {groupName}");
        //}

        public Task Echo(string message)
        {
            return Clients.Client(Context.ConnectionId).SendAsync("Send", $"{Context.ConnectionId}: {message}");
        }
    }
}
